//package com.china.produce.config.datasource;
//
//import com.china.produce.config.db.DBRouterConfig;
//import com.china.produce.dynamic.MybatisPlugin;
//import com.china.produce.system.util.PropertyUtil;
//import org.apache.ibatis.plugin.Interceptor;
//import org.springframework.context.EnvironmentAware;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.env.Environment;
//import org.springframework.jdbc.datasource.DriverManagerDataSource;
//import javax.sql.DataSource;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Optional;
//
//@Configuration
//public class DataSourceAutoConfig implements EnvironmentAware {
//
//    /**
//     * 分库数量
//     */
//    private int dbCount;
//
//    /**
//     * 分表数量
//     */
//    private int tbCount;
//
//    /**
//     * 路由字段
//     */
//    private String routeKey;
//
//    private  final Map<String, Map<String, Object>>  datasourceMaps = new HashMap<>();
//
//    private Map<String, Object> defaultDatasourceMap = new HashMap<>();
//
//    private static final String DATASOURCE_PREFIX = "db-router.jdbc.datasource.";
//
//    @Bean
//    public DBRouterConfig dbRouterConfig() {
//        return new DBRouterConfig(dbCount, tbCount, routeKey);
//    }
//
//    @Bean
//    public Interceptor plugin() {
//        return new MybatisPlugin();
//    }
//
//    @Bean("multiDataSource")
//    public DataSource dataSource() {
//        Map<Object, Object> targetDataSources = new HashMap<>();
//        for (String dbInfo : datasourceMaps.keySet()) {
//            Map<String, Object> objMap = datasourceMaps.get(dbInfo);
//
//            targetDataSources.put(dbInfo, new DriverManagerDataSource(objMap.get("url").toString(), objMap.get("username").toString(), objMap.get("password").toString()));
//        }
//
//        DynamicDataSource dynamicDataSource = new DynamicDataSource();
//        dynamicDataSource.setTargetDataSources(targetDataSources);
//        dynamicDataSource.setDefaultTargetDataSource(new DriverManagerDataSource(defaultDatasourceMap.get("url").toString(), defaultDatasourceMap.get("username").toString(), defaultDatasourceMap.get("password").toString()));
//
//        return dynamicDataSource;
//    }
//
//    /**
//     * 实现environmentAware 获取环境变量定义参数
//     *
//     * @param environment
//     */
//    @Override
//    public void setEnvironment(Environment environment) {
//
//        dbCount = Integer.parseInt(Optional.ofNullable(environment.getProperty(DATASOURCE_PREFIX + "dbCount")).orElse("0"));
//        tbCount = Integer.parseInt(Optional.ofNullable(environment.getProperty(DATASOURCE_PREFIX + "tbCount")).orElse("0"));
//        routeKey = environment.getProperty(DATASOURCE_PREFIX + "routeKey");
//
//        // 分库分表数据源
//        String dataSources = environment.getProperty(DATASOURCE_PREFIX + "list");
//        for (String dbInfo : dataSources.split(",")) {
//            Map<String, Object> handle = PropertyUtil.handle(environment, DATASOURCE_PREFIX + dbInfo, Map.class);
//
//            datasourceMaps.put(dbInfo, handle);
//        }
//
//        // 默认数据源
//       defaultDatasourceMap = PropertyUtil.handle(environment, DATASOURCE_PREFIX + environment.getProperty(DATASOURCE_PREFIX + "default"), Map.class);
//    }
//}
